package com.talent.service.impl;

import com.talent.mapper.HomeSettingsMapper;
import com.talent.mapper.HotSectionMapper;
import com.talent.pojo.dto.HomeSettingsDTO;
import com.talent.pojo.entity.HomeSettings;
import com.talent.pojo.entity.HotSection;
import com.talent.pojo.vo.HomeSettingsVO;
import com.talent.service.HomeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 首页管理Service实现类
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {

    @Autowired
    private HomeSettingsMapper homeSettingsMapper;

    @Autowired
    private HotSectionMapper hotSectionMapper;

    /**
     * 获取首页设置
     */
    @Override
    public HomeSettingsVO getHomeSettings() {
        log.info("获取首页设置");

        HomeSettingsVO homeSettingsVO = new HomeSettingsVO();

        // 获取首页基本设置
        HomeSettings homeSettings = homeSettingsMapper.getHomeSettings();
        if (homeSettings != null) {
            homeSettingsVO.setTitle(homeSettings.getTitle());
            homeSettingsVO.setBannerUrl(homeSettings.getBannerUrl());
        } else {
            // 如果没有设置，返回默认值
            homeSettingsVO.setTitle("人才地图管理系统");
            homeSettingsVO.setBannerUrl("");
        }

        // 获取热门栏目设置
        List<HotSection> hotSections = hotSectionMapper.getHotSections();
        List<HomeSettingsVO.HotSectionVO> hotSectionVOs = new ArrayList<>();

        if (hotSections != null && !hotSections.isEmpty()) {
            hotSectionVOs = hotSections.stream().map(hotSection -> {
                HomeSettingsVO.HotSectionVO hotSectionVO = new HomeSettingsVO.HotSectionVO();
                BeanUtils.copyProperties(hotSection, hotSectionVO);
                return hotSectionVO;
            }).collect(Collectors.toList());
        } else {
            // 如果没有热门栏目，返回4个空的栏目
            for (int i = 0; i < 4; i++) {
                HomeSettingsVO.HotSectionVO hotSectionVO = new HomeSettingsVO.HotSectionVO();
                hotSectionVO.setTitle("");
                hotSectionVO.setImageUrl("");
                hotSectionVO.setTargetType("");
                hotSectionVO.setTargetValue("");
                hotSectionVO.setSortOrder(i + 1);
                hotSectionVOs.add(hotSectionVO);
            }
        }

        homeSettingsVO.setHotSections(hotSectionVOs);
        return homeSettingsVO;
    }

    /**
     * 保存首页设置
     */
    @Override
    @Transactional
    public void saveHomeSettings(HomeSettingsDTO homeSettingsDTO) {
        log.info("保存首页设置：{}", homeSettingsDTO);

        LocalDateTime now = LocalDateTime.now();

        // 保存首页基本设置
        HomeSettings existingSettings = homeSettingsMapper.getHomeSettings();
        if (existingSettings != null) {
            // 更新现有设置
            existingSettings.setTitle(homeSettingsDTO.getTitle());
            existingSettings.setBannerUrl(homeSettingsDTO.getBannerUrl());
            existingSettings.setUpdateTime(now);
            homeSettingsMapper.updateHomeSettings(existingSettings);
        } else {
            // 创建新设置
            HomeSettings newSettings = new HomeSettings();
            newSettings.setTitle(homeSettingsDTO.getTitle());
            newSettings.setBannerUrl(homeSettingsDTO.getBannerUrl());
            newSettings.setCreateTime(now);
            newSettings.setUpdateTime(now);
            homeSettingsMapper.saveHomeSettings(newSettings);
        }

        // 保存热门栏目设置
        if (homeSettingsDTO.getHotSections() != null && !homeSettingsDTO.getHotSections().isEmpty()) {
            // 先删除所有现有的热门栏目
            hotSectionMapper.deleteAllHotSections();

            // 保存新的热门栏目
            List<HotSection> hotSections = new ArrayList<>();
            for (int i = 0; i < homeSettingsDTO.getHotSections().size(); i++) {
                HomeSettingsDTO.HotSectionDTO hotSectionDTO = homeSettingsDTO.getHotSections().get(i);
                
                // 只保存有内容的栏目
                if (hotSectionDTO.getImageUrl() != null && !hotSectionDTO.getImageUrl().trim().isEmpty()) {
                    HotSection hotSection = new HotSection();
                    BeanUtils.copyProperties(hotSectionDTO, hotSection);
                    hotSection.setSortOrder(i + 1);
                    hotSection.setCreateTime(now);
                    hotSection.setUpdateTime(now);
                    hotSections.add(hotSection);
                }
            }

            if (!hotSections.isEmpty()) {
                hotSectionMapper.batchSaveHotSections(hotSections);
            }
        }

        log.info("首页设置保存成功");
    }
}
